﻿using Root.Models.BLL;
using Root.Properties;
using SQLHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Root.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        public ActionResult Index()
        {
            return View();
        }

        public void Login()
        {
            Response.ContentType = "text/json";
            string userNameOrId = "";
            string password = null;
            int id = 0;
            string dbPassword = "";
            bool isCookieLogin = false;
            if (Request.Cookies.Get("Id") == null && Request.Cookies.Get("Password") == null)
            {
                isCookieLogin = false;
                userNameOrId = string.IsNullOrEmpty(Request["userNameOrId"]) ? "" : HttpUtility.UrlDecode(Request["userNameOrId"]);
                password = Request["password"];
            }
            else
            {
                isCookieLogin = true;
                userNameOrId = Request.Cookies.Get("userNameOrId").Value;
                password = Request.Cookies.Get("password").Value;
            }
            SQLiteHelper sql = new SQLiteHelper(Resources.SQLite_Root);
            SQLParameters.SQLite p = new SQLParameters.SQLite();
            if (Int32.TryParse(userNameOrId, out id))
            {
                if (isCookieLogin)
                {
                    dbPassword = CommonHelper.Hash.MD5((string)sql.ExecuteScalar("select Password from User where Id=@Id", p.New("@Id", id)) + Resources.SALT);
                }
                else
                {
                    dbPassword = (string)sql.ExecuteScalar("select Password from User where Id=@Id", p.New("@Id", id));
                }
            }
            else
            {
                if (isCookieLogin)
                {
                    dbPassword = CommonHelper.Hash.MD5((string)sql.ExecuteScalar("select Password from User where Name=@Name", p.New("@Name", userNameOrId.Trim())) + Resources.SALT);
                }
                else
                {
                    dbPassword = (string)sql.ExecuteScalar("select Password from User where Name=@Name", p.New("@Name", userNameOrId.Trim()));
                }
            }
            if (password == dbPassword && password != null)
            {
                Response.Write(new { status = "OK" });
                HttpCookie cookie_1 = new HttpCookie("userNameOrId", id.ToString());
                HttpCookie cookie_2 = new HttpCookie("password", isCookieLogin ? password : CommonHelper.Hash.MD5(password + Resources.SALT));
                cookie_1.Expires = DateTime.Now.AddYears(1);
                cookie_1.Expires = DateTime.Now.AddYears(1);
                Response.SetCookie(cookie_1);
                Response.SetCookie(cookie_2);
            }
            else
            {
                Response.Write(new { status = "ERROR" });
            }
        }
        public void Register()
        {

        }
    }
}